<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Histogram Comparison</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d7/da8/tutorial_table_of_content_imgproc.html">Image Processing (imgproc module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Histogram Comparison </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d8/dbc/tutorial_histogram_calculation.html">Histogram Calculation</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../da/d7f/tutorial_back_projection.html">Back Projection</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Ana Huamán </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Goal </h2>
<p>In this tutorial you will learn how to:</p>
<ul>
<li>Use the function <a class="el" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">cv::compareHist</a> to get a numerical parameter that express how well two histograms match with each other.</li>
<li>Use different metrics to compare histograms</li>
</ul>
<h2>Theory </h2>
<ul>
<li>To compare two histograms ( \(H_{1}\) and \(H_{2}\) ), first we have to choose a <em>metric</em> ( \(d(H_{1}, H_{2})\)) to express how well both histograms match.</li>
<li>OpenCV implements the function <a class="el" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">cv::compareHist</a> to perform a comparison. It also offers 4 different metrics to compute the matching:<ol type="1">
<li><b>Correlation ( CV_COMP_CORREL )</b> <p class="formulaDsp">
\[d(H_1,H_2) = \frac{\sum_I (H_1(I) - \bar{H_1}) (H_2(I) - \bar{H_2})}{\sqrt{\sum_I(H_1(I) - \bar{H_1})^2 \sum_I(H_2(I) - \bar{H_2})^2}}\]
</p>
 where <p class="formulaDsp">
\[\bar{H_k} = \frac{1}{N} \sum _J H_k(J)\]
</p>
 and \(N\) is the total number of histogram bins.</li>
<li><b>Chi-Square ( CV_COMP_CHISQR )</b> <p class="formulaDsp">
\[d(H_1,H_2) = \sum _I \frac{\left(H_1(I)-H_2(I)\right)^2}{H_1(I)}\]
</p>
</li>
<li><b>Intersection ( method=CV_COMP_INTERSECT )</b> <p class="formulaDsp">
\[d(H_1,H_2) = \sum _I \min (H_1(I), H_2(I))\]
</p>
</li>
<li><b>Bhattacharyya distance ( CV_COMP_BHATTACHARYYA )</b> <p class="formulaDsp">
\[d(H_1,H_2) = \sqrt{1 - \frac{1}{\sqrt{\bar{H_1} \bar{H_2} N^2}} \sum_I \sqrt{H_1(I) \cdot H_2(I)}}\]
</p>
</li>
</ol>
</li>
</ul>
<h2>Code </h2>
<ul>
<li><b>What does this program do?</b><ul>
<li>Loads a <em>base image</em> and 2 <em>test images</em> to be compared with it.</li>
<li>Generate 1 image that is the lower half of the <em>base image</em></li>
<li>Convert the images to HSV format</li>
<li>Calculate the H-S histogram for all the images and normalize them in order to compare them.</li>
<li>Compare the histogram of the <em>base image</em> with respect to the 2 test histograms, the histogram of the lower half base image and with the same base image histogram.</li>
<li>Display the numerical matching parameters obtained.</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/Histograms_Matching/compareHist_Demo.cpp">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* keys =</div><div class="line">    <span class="stringliteral">&quot;{ help  h| | Print help message. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ @input1 | | Path to input image 1. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ @input2 | | Path to input image 2. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ @input3 | | Path to input image 3. }&quot;</span>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv )</div><div class="line">{</div><div class="line">    <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser( argc, argv, keys );</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src_base = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;input1&quot;</span>) );</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src_test1 = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;input2&quot;</span>) );</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src_test2 = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;input3&quot;</span>) );</div><div class="line">    <span class="keywordflow">if</span>( src_base.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() || src_test1.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() || src_test2.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() )</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Could not open or find the images!\n&quot;</span> &lt;&lt; endl;</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> hsv_base, hsv_test1, hsv_test2;</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( src_base, hsv_base, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a> );</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( src_test1, hsv_test1, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a> );</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( src_test2, hsv_test2, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a> );</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> hsv_half_down = hsv_base( <a class="code" href="../../da/d35/classcv_1_1Range.html">Range</a>( hsv_base.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>/2, hsv_base.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a> ), <a class="code" href="../../da/d35/classcv_1_1Range.html">Range</a>( 0, hsv_base.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> ) );</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> h_bins = 50, s_bins = 60;</div><div class="line">    <span class="keywordtype">int</span> histSize[] = { h_bins, s_bins };</div><div class="line"></div><div class="line">    <span class="comment">// hue varies from 0 to 179, saturation from 0 to 255</span></div><div class="line">    <span class="keywordtype">float</span> h_ranges[] = { 0, 180 };</div><div class="line">    <span class="keywordtype">float</span> s_ranges[] = { 0, 256 };</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">float</span>* ranges[] = { h_ranges, s_ranges };</div><div class="line"></div><div class="line">    <span class="comment">// Use the 0-th and 1-st channels</span></div><div class="line">    <span class="keywordtype">int</span> channels[] = { 0, 1 };</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> hist_base, hist_half_down, hist_test1, hist_test2;</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>( &amp;hsv_base, 1, channels, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(), hist_base, 2, histSize, ranges, <span class="keyword">true</span>, <span class="keyword">false</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( hist_base, hist_base, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>() );</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>( &amp;hsv_half_down, 1, channels, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(), hist_half_down, 2, histSize, ranges, <span class="keyword">true</span>, <span class="keyword">false</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( hist_half_down, hist_half_down, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>() );</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>( &amp;hsv_test1, 1, channels, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(), hist_test1, 2, histSize, ranges, <span class="keyword">true</span>, <span class="keyword">false</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( hist_test1, hist_test1, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>() );</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>( &amp;hsv_test2, 1, channels, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(), hist_test2, 2, histSize, ranges, <span class="keyword">true</span>, <span class="keyword">false</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( hist_test2, hist_test2, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>() );</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">int</span> compare_method = 0; compare_method &lt; 4; compare_method++ )</div><div class="line">    {</div><div class="line">        <span class="keywordtype">double</span> base_base = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">compareHist</a>( hist_base, hist_base, compare_method );</div><div class="line">        <span class="keywordtype">double</span> base_half = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">compareHist</a>( hist_base, hist_half_down, compare_method );</div><div class="line">        <span class="keywordtype">double</span> base_test1 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">compareHist</a>( hist_base, hist_test1, compare_method );</div><div class="line">        <span class="keywordtype">double</span> base_test2 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">compareHist</a>( hist_base, hist_test2, compare_method );</div><div class="line"></div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Method &quot;</span> &lt;&lt; compare_method &lt;&lt; <span class="stringliteral">&quot; Perfect, Base-Half, Base-Test(1), Base-Test(2) : &quot;</span></div><div class="line">             &lt;&lt;  base_base &lt;&lt; <span class="stringliteral">&quot; / &quot;</span> &lt;&lt; base_half &lt;&lt; <span class="stringliteral">&quot; / &quot;</span> &lt;&lt; base_test1 &lt;&lt; <span class="stringliteral">&quot; / &quot;</span> &lt;&lt; base_test2 &lt;&lt; endl;</div><div class="line">    }</div><div class="line"></div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Done \n&quot;</span>;</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/Histograms_Matching/histogram_comparison/CompareHistDemo.java">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> java.util.Arrays;</div><div class="line"><span class="keyword">import</span> java.util.List;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.opencv.core.Core;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"><span class="keyword">import</span> org.opencv.core.MatOfFloat;</div><div class="line"><span class="keyword">import</span> org.opencv.core.MatOfInt;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Range;</div><div class="line"><span class="keyword">import</span> org.opencv.imgcodecs.Imgcodecs;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"></div><div class="line"><span class="keyword">class </span>CompareHist {</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="keywordflow">if</span> (args.length != 3) {</div><div class="line">            System.err.println(<span class="stringliteral">&quot;You must supply 3 arguments that correspond to the paths to 3 images.&quot;</span>);</div><div class="line">            System.exit(0);</div><div class="line">        }</div><div class="line">        Mat srcBase = Imgcodecs.imread(args[0]);</div><div class="line">        Mat srcTest1 = Imgcodecs.imread(args[1]);</div><div class="line">        Mat srcTest2 = Imgcodecs.imread(args[2]);</div><div class="line">        <span class="keywordflow">if</span> (srcBase.empty() || srcTest1.empty() || srcTest2.empty()) {</div><div class="line">            System.err.println(<span class="stringliteral">&quot;Cannot read the images&quot;</span>);</div><div class="line">            System.exit(0);</div><div class="line">        }</div><div class="line"></div><div class="line">        Mat hsvBase = <span class="keyword">new</span> Mat(), hsvTest1 = <span class="keyword">new</span> Mat(), hsvTest2 = <span class="keyword">new</span> Mat();</div><div class="line">        Imgproc.cvtColor( srcBase, hsvBase, Imgproc.COLOR_BGR2HSV );</div><div class="line">        Imgproc.cvtColor( srcTest1, hsvTest1, Imgproc.COLOR_BGR2HSV );</div><div class="line">        Imgproc.cvtColor( srcTest2, hsvTest2, Imgproc.COLOR_BGR2HSV );</div><div class="line"></div><div class="line">        Mat hsvHalfDown = hsvBase.submat( <span class="keyword">new</span> Range( hsvBase.rows()/2, hsvBase.rows() - 1 ), <span class="keyword">new</span> Range( 0, hsvBase.cols() - 1 ) );</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> hBins = 50, sBins = 60;</div><div class="line">        <span class="keywordtype">int</span>[] histSize = { hBins, sBins };</div><div class="line"></div><div class="line">        <span class="comment">// hue varies from 0 to 179, saturation from 0 to 255</span></div><div class="line">        <span class="keywordtype">float</span>[] ranges = { 0, 180, 0, 256 };</div><div class="line"></div><div class="line">        <span class="comment">// Use the 0-th and 1-st channels</span></div><div class="line">        <span class="keywordtype">int</span>[] channels = { 0, 1 };</div><div class="line"></div><div class="line">        Mat histBase = <span class="keyword">new</span> Mat(), histHalfDown = <span class="keyword">new</span> Mat(), histTest1 = <span class="keyword">new</span> Mat(), histTest2 = <span class="keyword">new</span> Mat();</div><div class="line"></div><div class="line">        List&lt;Mat&gt; hsvBaseList = Arrays.asList(hsvBase);</div><div class="line">        Imgproc.calcHist(hsvBaseList, <span class="keyword">new</span> MatOfInt(channels), <span class="keyword">new</span> Mat(), histBase, <span class="keyword">new</span> MatOfInt(histSize), <span class="keyword">new</span> MatOfFloat(ranges), <span class="keyword">false</span>);</div><div class="line">        Core.normalize(histBase, histBase, 0, 1, Core.NORM_MINMAX);</div><div class="line"></div><div class="line">        List&lt;Mat&gt; hsvHalfDownList = Arrays.asList(hsvHalfDown);</div><div class="line">        Imgproc.calcHist(hsvHalfDownList, <span class="keyword">new</span> MatOfInt(channels), <span class="keyword">new</span> Mat(), histHalfDown, <span class="keyword">new</span> MatOfInt(histSize), <span class="keyword">new</span> MatOfFloat(ranges), <span class="keyword">false</span>);</div><div class="line">        Core.normalize(histHalfDown, histHalfDown, 0, 1, Core.NORM_MINMAX);</div><div class="line"></div><div class="line">        List&lt;Mat&gt; hsvTest1List = Arrays.asList(hsvTest1);</div><div class="line">        Imgproc.calcHist(hsvTest1List, <span class="keyword">new</span> MatOfInt(channels), <span class="keyword">new</span> Mat(), histTest1, <span class="keyword">new</span> MatOfInt(histSize), <span class="keyword">new</span> MatOfFloat(ranges), <span class="keyword">false</span>);</div><div class="line">        Core.normalize(histTest1, histTest1, 0, 1, Core.NORM_MINMAX);</div><div class="line"></div><div class="line">        List&lt;Mat&gt; hsvTest2List = Arrays.asList(hsvTest2);</div><div class="line">        Imgproc.calcHist(hsvTest2List, <span class="keyword">new</span> MatOfInt(channels), <span class="keyword">new</span> Mat(), histTest2, <span class="keyword">new</span> MatOfInt(histSize), <span class="keyword">new</span> MatOfFloat(ranges), <span class="keyword">false</span>);</div><div class="line">        Core.normalize(histTest2, histTest2, 0, 1, Core.NORM_MINMAX);</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span>( <span class="keywordtype">int</span> compareMethod = 0; compareMethod &lt; 4; compareMethod++ ) {</div><div class="line">            <span class="keywordtype">double</span> baseBase = Imgproc.compareHist( histBase, histBase, compareMethod );</div><div class="line">            <span class="keywordtype">double</span> baseHalf = Imgproc.compareHist( histBase, histHalfDown, compareMethod );</div><div class="line">            <span class="keywordtype">double</span> baseTest1 = Imgproc.compareHist( histBase, histTest1, compareMethod );</div><div class="line">            <span class="keywordtype">double</span> baseTest2 = Imgproc.compareHist( histBase, histTest2, compareMethod );</div><div class="line"></div><div class="line">            System.out.println(<span class="stringliteral">&quot;Method &quot;</span> + compareMethod + <span class="stringliteral">&quot; Perfect, Base-Half, Base-Test(1), Base-Test(2) : &quot;</span> + baseBase + <span class="stringliteral">&quot; / &quot;</span> + baseHalf</div><div class="line">                    + <span class="stringliteral">&quot; / &quot;</span> + baseTest1 + <span class="stringliteral">&quot; / &quot;</span> + baseTest2);</div><div class="line">        }</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>CompareHistDemo {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="comment">// Load the native OpenCV library</span></div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line"></div><div class="line">        <span class="keyword">new</span> CompareHist().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/Histograms_Matching/histogram_comparison/compareHist_Demo.py">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> print_function</div><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> division</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> argparse</div><div class="line"></div><div class="line"></div><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;Code for Histogram Comparison tutorial.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--input1&#39;</span>, help=<span class="stringliteral">&#39;Path to input image 1.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--input2&#39;</span>, help=<span class="stringliteral">&#39;Path to input image 2.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--input3&#39;</span>, help=<span class="stringliteral">&#39;Path to input image 3.&#39;</span>)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line">src_base = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(args.input1)</div><div class="line">src_test1 = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(args.input2)</div><div class="line">src_test2 = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(args.input3)</div><div class="line"><span class="keywordflow">if</span> src_base <span class="keywordflow">is</span> <span class="keywordtype">None</span> <span class="keywordflow">or</span> src_test1 <span class="keywordflow">is</span> <span class="keywordtype">None</span> <span class="keywordflow">or</span> src_test2 <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Could not open or find the images!&#39;</span>)</div><div class="line">    exit(0)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">hsv_base = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(src_base, cv.COLOR_BGR2HSV)</div><div class="line">hsv_test1 = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(src_test1, cv.COLOR_BGR2HSV)</div><div class="line">hsv_test2 = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(src_test2, cv.COLOR_BGR2HSV)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">hsv_half_down = hsv_base[hsv_base.shape[0]//2:,:]</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">h_bins = 50</div><div class="line">s_bins = 60</div><div class="line">histSize = [h_bins, s_bins]</div><div class="line"></div><div class="line"><span class="comment"># hue varies from 0 to 179, saturation from 0 to 255</span></div><div class="line">h_ranges = [0, 180]</div><div class="line">s_ranges = [0, 256]</div><div class="line">ranges = h_ranges + s_ranges <span class="comment"># concat lists</span></div><div class="line"></div><div class="line"><span class="comment"># Use the 0-th and 1-st channels</span></div><div class="line">channels = [0, 1]</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">hist_base = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_base], channels, <span class="keywordtype">None</span>, histSize, ranges, accumulate=<span class="keyword">False</span>)</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(hist_base, hist_base, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)</div><div class="line"></div><div class="line">hist_half_down = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_half_down], channels, <span class="keywordtype">None</span>, histSize, ranges, accumulate=<span class="keyword">False</span>)</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(hist_half_down, hist_half_down, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)</div><div class="line"></div><div class="line">hist_test1 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_test1], channels, <span class="keywordtype">None</span>, histSize, ranges, accumulate=<span class="keyword">False</span>)</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(hist_test1, hist_test1, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)</div><div class="line"></div><div class="line">hist_test2 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_test2], channels, <span class="keywordtype">None</span>, histSize, ranges, accumulate=<span class="keyword">False</span>)</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(hist_test2, hist_test2, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordflow">for</span> compare_method <span class="keywordflow">in</span> range(4):</div><div class="line">    base_base = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga41a249ba9a536bf2b47e95f8c7dd42b0">cv.compareHist</a>(hist_base, hist_base, compare_method)</div><div class="line">    base_half = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga41a249ba9a536bf2b47e95f8c7dd42b0">cv.compareHist</a>(hist_base, hist_half_down, compare_method)</div><div class="line">    base_test1 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga41a249ba9a536bf2b47e95f8c7dd42b0">cv.compareHist</a>(hist_base, hist_test1, compare_method)</div><div class="line">    base_test2 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga41a249ba9a536bf2b47e95f8c7dd42b0">cv.compareHist</a>(hist_base, hist_test2, compare_method)</div><div class="line"></div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Method:&#39;</span>, compare_method, <span class="stringliteral">&#39;Perfect, Base-Half, Base-Test(1), Base-Test(2) :&#39;</span>,\</div><div class="line">          base_base, <span class="stringliteral">&#39;/&#39;</span>, base_half, <span class="stringliteral">&#39;/&#39;</span>, base_test1, <span class="stringliteral">&#39;/&#39;</span>, base_test2)</div><div class="line"></div></div><!-- fragment -->  </div> </li>
</ul>
<h2>Explanation </h2>
<ul>
<li>Load the base image (src_base) and the other two test images:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    CommandLineParser parser( argc, argv, keys );</div><div class="line">    Mat src_base = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;input1&quot;</span>) );</div><div class="line">    Mat src_test1 = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;input2&quot;</span>) );</div><div class="line">    Mat src_test2 = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;input3&quot;</span>) );</div><div class="line">    <span class="keywordflow">if</span>( src_base.empty() || src_test1.empty() || src_test2.empty() )</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Could not open or find the images!\n&quot;</span> &lt;&lt; endl;</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">if</span> (args.length != 3) {</div><div class="line">            System.err.println(<span class="stringliteral">&quot;You must supply 3 arguments that correspond to the paths to 3 images.&quot;</span>);</div><div class="line">            System.exit(0);</div><div class="line">        }</div><div class="line">        Mat srcBase = Imgcodecs.imread(args[0]);</div><div class="line">        Mat srcTest1 = Imgcodecs.imread(args[1]);</div><div class="line">        Mat srcTest2 = Imgcodecs.imread(args[2]);</div><div class="line">        <span class="keywordflow">if</span> (srcBase.empty() || srcTest1.empty() || srcTest2.empty()) {</div><div class="line">            System.err.println(<span class="stringliteral">&quot;Cannot read the images&quot;</span>);</div><div class="line">            System.exit(0);</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;Code for Histogram Comparison tutorial.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--input1&#39;</span>, help=<span class="stringliteral">&#39;Path to input image 1.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--input2&#39;</span>, help=<span class="stringliteral">&#39;Path to input image 2.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--input3&#39;</span>, help=<span class="stringliteral">&#39;Path to input image 3.&#39;</span>)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line">src_base = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(args.input1)</div><div class="line">src_test1 = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(args.input2)</div><div class="line">src_test2 = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(args.input3)</div><div class="line"><span class="keywordflow">if</span> src_base <span class="keywordflow">is</span> <span class="keywordtype">None</span> <span class="keywordflow">or</span> src_test1 <span class="keywordflow">is</span> <span class="keywordtype">None</span> <span class="keywordflow">or</span> src_test2 <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Could not open or find the images!&#39;</span>)</div><div class="line">    exit(0)</div></div><!-- fragment --> </div> <ul>
<li>Convert them to HSV format:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat hsv_base, hsv_test1, hsv_test2;</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( src_base, hsv_base, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a> );</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( src_test1, hsv_test1, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a> );</div><div class="line">    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( src_test2, hsv_test2, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a> );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Mat hsvBase = <span class="keyword">new</span> Mat(), hsvTest1 = <span class="keyword">new</span> Mat(), hsvTest2 = <span class="keyword">new</span> Mat();</div><div class="line">        Imgproc.cvtColor( srcBase, hsvBase, Imgproc.COLOR_BGR2HSV );</div><div class="line">        Imgproc.cvtColor( srcTest1, hsvTest1, Imgproc.COLOR_BGR2HSV );</div><div class="line">        Imgproc.cvtColor( srcTest2, hsvTest2, Imgproc.COLOR_BGR2HSV );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">hsv_base = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(src_base, cv.COLOR_BGR2HSV)</div><div class="line">hsv_test1 = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(src_test1, cv.COLOR_BGR2HSV)</div><div class="line">hsv_test2 = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(src_test2, cv.COLOR_BGR2HSV)</div></div><!-- fragment --> </div> <ul>
<li>Also, create an image of half the base image (in HSV format):</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows ), Range( 0, hsv_base.cols ) );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Mat hsvHalfDown = hsvBase.submat( <span class="keyword">new</span> Range( hsvBase.rows()/2, hsvBase.rows() - 1 ), <span class="keyword">new</span> Range( 0, hsvBase.cols() - 1 ) );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">hsv_half_down = hsv_base[hsv_base.shape[0]//2:,:]</div></div><!-- fragment --> </div> <ul>
<li>Initialize the arguments to calculate the histograms (bins, ranges and channels H and S ).</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordtype">int</span> h_bins = 50, s_bins = 60;</div><div class="line">    <span class="keywordtype">int</span> histSize[] = { h_bins, s_bins };</div><div class="line"></div><div class="line">    <span class="comment">// hue varies from 0 to 179, saturation from 0 to 255</span></div><div class="line">    <span class="keywordtype">float</span> h_ranges[] = { 0, 180 };</div><div class="line">    <span class="keywordtype">float</span> s_ranges[] = { 0, 256 };</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">float</span>* ranges[] = { h_ranges, s_ranges };</div><div class="line"></div><div class="line">    <span class="comment">// Use the 0-th and 1-st channels</span></div><div class="line">    <span class="keywordtype">int</span> channels[] = { 0, 1 };</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordtype">int</span> hBins = 50, sBins = 60;</div><div class="line">        <span class="keywordtype">int</span>[] histSize = { hBins, sBins };</div><div class="line"></div><div class="line">        <span class="comment">// hue varies from 0 to 179, saturation from 0 to 255</span></div><div class="line">        <span class="keywordtype">float</span>[] ranges = { 0, 180, 0, 256 };</div><div class="line"></div><div class="line">        <span class="comment">// Use the 0-th and 1-st channels</span></div><div class="line">        <span class="keywordtype">int</span>[] channels = { 0, 1 };</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">h_bins = 50</div><div class="line">s_bins = 60</div><div class="line">histSize = [h_bins, s_bins]</div><div class="line"></div><div class="line"><span class="comment"># hue varies from 0 to 179, saturation from 0 to 255</span></div><div class="line">h_ranges = [0, 180]</div><div class="line">s_ranges = [0, 256]</div><div class="line">ranges = h_ranges + s_ranges <span class="comment"># concat lists</span></div><div class="line"></div><div class="line"><span class="comment"># Use the 0-th and 1-st channels</span></div><div class="line">channels = [0, 1]</div></div><!-- fragment --> </div> <ul>
<li>Calculate the Histograms for the base image, the 2 test images and the half-down base image:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat hist_base, hist_half_down, hist_test1, hist_test2;</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>( &amp;hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, <span class="keyword">true</span>, <span class="keyword">false</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( hist_base, hist_base, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, Mat() );</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>( &amp;hsv_half_down, 1, channels, Mat(), hist_half_down, 2, histSize, ranges, <span class="keyword">true</span>, <span class="keyword">false</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( hist_half_down, hist_half_down, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, Mat() );</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>( &amp;hsv_test1, 1, channels, Mat(), hist_test1, 2, histSize, ranges, <span class="keyword">true</span>, <span class="keyword">false</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( hist_test1, hist_test1, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, Mat() );</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>( &amp;hsv_test2, 1, channels, Mat(), hist_test2, 2, histSize, ranges, <span class="keyword">true</span>, <span class="keyword">false</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( hist_test2, hist_test2, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, Mat() );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Mat histBase = <span class="keyword">new</span> Mat(), histHalfDown = <span class="keyword">new</span> Mat(), histTest1 = <span class="keyword">new</span> Mat(), histTest2 = <span class="keyword">new</span> Mat();</div><div class="line"></div><div class="line">        List&lt;Mat&gt; hsvBaseList = Arrays.asList(hsvBase);</div><div class="line">        Imgproc.calcHist(hsvBaseList, <span class="keyword">new</span> MatOfInt(channels), <span class="keyword">new</span> Mat(), histBase, <span class="keyword">new</span> MatOfInt(histSize), <span class="keyword">new</span> MatOfFloat(ranges), <span class="keyword">false</span>);</div><div class="line">        Core.normalize(histBase, histBase, 0, 1, Core.NORM_MINMAX);</div><div class="line"></div><div class="line">        List&lt;Mat&gt; hsvHalfDownList = Arrays.asList(hsvHalfDown);</div><div class="line">        Imgproc.calcHist(hsvHalfDownList, <span class="keyword">new</span> MatOfInt(channels), <span class="keyword">new</span> Mat(), histHalfDown, <span class="keyword">new</span> MatOfInt(histSize), <span class="keyword">new</span> MatOfFloat(ranges), <span class="keyword">false</span>);</div><div class="line">        Core.normalize(histHalfDown, histHalfDown, 0, 1, Core.NORM_MINMAX);</div><div class="line"></div><div class="line">        List&lt;Mat&gt; hsvTest1List = Arrays.asList(hsvTest1);</div><div class="line">        Imgproc.calcHist(hsvTest1List, <span class="keyword">new</span> MatOfInt(channels), <span class="keyword">new</span> Mat(), histTest1, <span class="keyword">new</span> MatOfInt(histSize), <span class="keyword">new</span> MatOfFloat(ranges), <span class="keyword">false</span>);</div><div class="line">        Core.normalize(histTest1, histTest1, 0, 1, Core.NORM_MINMAX);</div><div class="line"></div><div class="line">        List&lt;Mat&gt; hsvTest2List = Arrays.asList(hsvTest2);</div><div class="line">        Imgproc.calcHist(hsvTest2List, <span class="keyword">new</span> MatOfInt(channels), <span class="keyword">new</span> Mat(), histTest2, <span class="keyword">new</span> MatOfInt(histSize), <span class="keyword">new</span> MatOfFloat(ranges), <span class="keyword">false</span>);</div><div class="line">        Core.normalize(histTest2, histTest2, 0, 1, Core.NORM_MINMAX);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">hist_base = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_base], channels, <span class="keywordtype">None</span>, histSize, ranges, accumulate=<span class="keyword">False</span>)</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(hist_base, hist_base, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)</div><div class="line"></div><div class="line">hist_half_down = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_half_down], channels, <span class="keywordtype">None</span>, histSize, ranges, accumulate=<span class="keyword">False</span>)</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(hist_half_down, hist_half_down, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)</div><div class="line"></div><div class="line">hist_test1 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_test1], channels, <span class="keywordtype">None</span>, histSize, ranges, accumulate=<span class="keyword">False</span>)</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(hist_test1, hist_test1, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)</div><div class="line"></div><div class="line">hist_test2 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a">cv.calcHist</a>([hsv_test2], channels, <span class="keywordtype">None</span>, histSize, ranges, accumulate=<span class="keyword">False</span>)</div><div class="line"><a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(hist_test2, hist_test2, alpha=0, beta=1, norm_type=cv.NORM_MINMAX)</div></div><!-- fragment --> </div> <ul>
<li>Apply sequentially the 4 comparison methods between the histogram of the base image (hist_base) and the other histograms:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">int</span> compare_method = 0; compare_method &lt; 4; compare_method++ )</div><div class="line">    {</div><div class="line">        <span class="keywordtype">double</span> base_base = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">compareHist</a>( hist_base, hist_base, compare_method );</div><div class="line">        <span class="keywordtype">double</span> base_half = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">compareHist</a>( hist_base, hist_half_down, compare_method );</div><div class="line">        <span class="keywordtype">double</span> base_test1 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">compareHist</a>( hist_base, hist_test1, compare_method );</div><div class="line">        <span class="keywordtype">double</span> base_test2 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd">compareHist</a>( hist_base, hist_test2, compare_method );</div><div class="line"></div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Method &quot;</span> &lt;&lt; compare_method &lt;&lt; <span class="stringliteral">&quot; Perfect, Base-Half, Base-Test(1), Base-Test(2) : &quot;</span></div><div class="line">             &lt;&lt;  base_base &lt;&lt; <span class="stringliteral">&quot; / &quot;</span> &lt;&lt; base_half &lt;&lt; <span class="stringliteral">&quot; / &quot;</span> &lt;&lt; base_test1 &lt;&lt; <span class="stringliteral">&quot; / &quot;</span> &lt;&lt; base_test2 &lt;&lt; endl;</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">for</span>( <span class="keywordtype">int</span> compareMethod = 0; compareMethod &lt; 4; compareMethod++ ) {</div><div class="line">            <span class="keywordtype">double</span> baseBase = Imgproc.compareHist( histBase, histBase, compareMethod );</div><div class="line">            <span class="keywordtype">double</span> baseHalf = Imgproc.compareHist( histBase, histHalfDown, compareMethod );</div><div class="line">            <span class="keywordtype">double</span> baseTest1 = Imgproc.compareHist( histBase, histTest1, compareMethod );</div><div class="line">            <span class="keywordtype">double</span> baseTest2 = Imgproc.compareHist( histBase, histTest2, compareMethod );</div><div class="line"></div><div class="line">            System.out.println(<span class="stringliteral">&quot;Method &quot;</span> + compareMethod + <span class="stringliteral">&quot; Perfect, Base-Half, Base-Test(1), Base-Test(2) : &quot;</span> + baseBase + <span class="stringliteral">&quot; / &quot;</span> + baseHalf</div><div class="line">                    + <span class="stringliteral">&quot; / &quot;</span> + baseTest1 + <span class="stringliteral">&quot; / &quot;</span> + baseTest2);</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><span class="keywordflow">for</span> compare_method <span class="keywordflow">in</span> range(4):</div><div class="line">    base_base = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga41a249ba9a536bf2b47e95f8c7dd42b0">cv.compareHist</a>(hist_base, hist_base, compare_method)</div><div class="line">    base_half = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga41a249ba9a536bf2b47e95f8c7dd42b0">cv.compareHist</a>(hist_base, hist_half_down, compare_method)</div><div class="line">    base_test1 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga41a249ba9a536bf2b47e95f8c7dd42b0">cv.compareHist</a>(hist_base, hist_test1, compare_method)</div><div class="line">    base_test2 = <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga41a249ba9a536bf2b47e95f8c7dd42b0">cv.compareHist</a>(hist_base, hist_test2, compare_method)</div><div class="line"></div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Method:&#39;</span>, compare_method, <span class="stringliteral">&#39;Perfect, Base-Half, Base-Test(1), Base-Test(2) :&#39;</span>,\</div><div class="line">          base_base, <span class="stringliteral">&#39;/&#39;</span>, base_half, <span class="stringliteral">&#39;/&#39;</span>, base_test1, <span class="stringliteral">&#39;/&#39;</span>, base_test2)</div></div><!-- fragment --> </div> <h2>Results </h2>
<ol type="1">
<li>We use as input the following images: <div class="image">
<img src="../../Histogram_Comparison_Source_0.jpg" alt="Histogram_Comparison_Source_0.jpg"/>
<div class="caption">
Base_0</div></div>
 <div class="image">
<img src="../../Histogram_Comparison_Source_1.jpg" alt="Histogram_Comparison_Source_1.jpg"/>
<div class="caption">
Test_1</div></div>
 <div class="image">
<img src="../../Histogram_Comparison_Source_2.jpg" alt="Histogram_Comparison_Source_2.jpg"/>
<div class="caption">
Test_2</div></div>
 where the first one is the base (to be compared to the others), the other 2 are the test images. We will also compare the first image with respect to itself and with respect of half the base image.</li>
<li>We should expect a perfect match when we compare the base image histogram with itself. Also, compared with the histogram of half the base image, it should present a high match since both are from the same source. For the other two test images, we can observe that they have very different lighting conditions, so the matching should not be very good:</li>
<li>Here the numeric results we got with OpenCV 3.4.1: <table class="doxtable">
<tr>
<th>*Method* </th><th>Base - Base </th><th>Base - Half </th><th>Base - Test 1 </th><th>Base - Test 2  </th></tr>
<tr>
<td>*Correlation* </td><td>1.000000 </td><td>0.880438 </td><td>0.20457 </td><td>0.0664547 </td></tr>
<tr>
<td>*Chi-square* </td><td>0.000000 </td><td>4.6834 </td><td>2697.98 </td><td>4763.8 </td></tr>
<tr>
<td>*Intersection* </td><td>18.8947 </td><td>13.022 </td><td>5.44085 </td><td>2.58173 </td></tr>
<tr>
<td>*Bhattacharyya* </td><td>0.000000 </td><td>0.237887 </td><td>0.679826 </td><td>0.874173 </td></tr>
</table>
For the <em>Correlation</em> and <em>Intersection</em> methods, the higher the metric, the more accurate the match. As we can see, the match <em>base-base</em> is the highest of all as expected. Also we can observe that the match <em>base-half</em> is the second best match (as we predicted). For the other two metrics, the less the result, the better the match. We can observe that the matches between the test 1 and test 2 with respect to the base are worse, which again, was expected. </li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:35 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
